//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    isShowLogin: false,
    routers: [
      {
        name: '世界之最',
        url: '/pages/world/list?datatype=1',
        icon: '/images/shijie.png'
      },
      {
        name: '中国之最',
        url: '/pages/world/list?datatype=2',
        icon: '/images/zhongguo.png'
      },
      {
        name: '人类之最',
        url: '/pages/world/list?datatype=3',
        icon: '/images/renlei.png'
      },
      {
        name: '美女之最',
        url: '/pages/world/list?datatype=4',
        icon: '/images/meinv.png'
      },
      {
        name: '自然之最',
        url: '/pages/world/list?datatype=5',
        icon: '/images/ziran.png'
      },
      {
        name: '动物之最',
        url: '/pages/world/list?datatype=6',
        icon: '/images/dongwu.png'
      },
      {
        name: '世界百科',
        url: '/pages/world/list?datatype=8',
        icon: '/images/baike.png'
      },
      {
        name: '世界奇闻',
        url: '/pages/world/list?datatype=9',
        icon: '/images/qiwen.png'
      },
      {
        name: '最近更新',
        url: '/pages/world/list?datatype=999',
        icon: '/images/new.png'
      }
    ]  
  },

  onLoad: function () {

    this.userLogin();
  },
  //检测登录
  userLogin: function () {
    // 查看是否授权
    var that = this;
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success: function (res) {

              that.setData({
                userInfo: res.userInfo
              })
              app.wxLogin(res.userInfo);
            }
          })
        } else {

          that.setData({
            isShowLogin: true
          })
        }
      }
    })
  },
  bindGetUserInfo: function (e) {
    var that = this;
    if (e.detail.userInfo == undefined) {
      //用户拒绝授权进行处理
      wx.showModal({
        title: '提示',
        content: '您点击了拒绝授权,将无法正常使用,点击确定重新获取授权。',
        success: function (res) {

        }
      })
    } else {
      //授权成功
      this.setData({
        isShowLogin: false
      })
      wx.getUserInfo({
        success: function (res) {

          that.setData({
            userInfo: res.userInfo
          })

        }
      })
      app.wxLogin(e.detail.userInfo);
    }
  },
  onShow:function(){
    wx.showShareMenu({
      withShareTicket: true
    })
  },

  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '世界之最刷新你的认知!',
      path: '/pages/index/index',
      success: function (res) {
        //getSystemInfo是为了获取当前设备信息，判断是android还是ios，如果是android
        //还需要调用wx.getShareInfo()，只有当成功回调才是转发群，ios就只需判断shareTickets
        wx.getSystemInfo({
          success: function (d) {
            // console.log(d.platform);
            //console.log(res.shareTickets);
            if (res.shareTickets == null) {

              return;
            }
            if (d.platform == 'android' && (res.shareTickets.length > 0)) {
              wx.getShareInfo({
                shareTicket: res.shareTickets,
                success: function () {
                  //console.log("安卓群");
                  //app.sharePage();
                  wx.showToast({
                    title: "分享成功",
                    duration: 2000
                  });
                },
                fail: function () {
                
                  wx.showToast({
                    title: "分享成功",
                    duration: 2000
                  });
                }
              })
              return;
            }
            if (d.platform == 'ios' && (res.shareTickets.length > 0)) {

              //console.log("IOS群");
              //app.sharePage();
              wx.showToast({
                title: "分享成功",
                duration: 2000
              });
            } else {
              wx.showToast({
                title: "分享成功",
                duration: 2000
              });
            }
          },
          fail: function (res) {
            wx.showToast({
              title: "转发失败",
              duration: 2000
            });
          }
        })
      },
      fail: function (res) {
        // 转发失败
        console.log("取消转发");
      }
    }
  }
  
})
